//========================================================================
/**
 * Author   : cuisw <shaovie@gmail.com>
 * Date     : 2008-11-11 11:46
 */
//========================================================================

#ifndef _NDK_OS_H_
#define _NDK_OS_H_
#include "Pre.h"

#include "GlobalMacros.h"
#include "DefaultConstants.h"

// Previous declare
#if defined NDK_LINUX
#   include <fcntl.h>
#   include <stdio.h>
#   include <unistd.h>
#   include <sys/uio.h>
#   include <sys/socket.h>
#elif defined NDK_WIN32
#endif

#if defined NDK_WIN32
// = For file stat
typedef struct _stat ndk_stat;
typedef int  mode_t;

enum
{
    O_CREAT	= 0x01,
    O_RDWR	= 0x02,
    O_RDONLY	= 0x04,
    O_WRONLY	= 0x08,
    O_APPEND	= 0x10,
    O_TRUNC	= 0x20,
    O_EXCL      = 0x40
};
// = For the scatter/gather type of I/O
// The ordering of the fields in this struct is important.  It has to
// match those in WSABUF.
struct iovec
{
    // byte count to read/write
    u_long iov_len;

    // data to be read/written
    char *iov_base;

    operator WSABUF &(void) { return *((WSABUF *) this); }
};
#elif defined NDK_LINUX
typedef struct stat ndk_stat;
#endif
/**
 * @class NDK_OS
 *
 * @brief 
 */
namespace NDK_OS
{
    extern NDK_HANDLE socket (int domain, int type, int protocol);

    extern int closesocket (NDK_HANDLE handle);

    extern int bind (NDK_HANDLE handle,
            struct sockaddr *addr,
            int addrlen);

    extern int listen (NDK_HANDLE handle,
            int backlog);

    extern NDK_HANDLE accept (NDK_HANDLE listen_handle,
            struct sockaddr *addr,
            int *addrlen);

    extern int connect (NDK_HANDLE handle,
            const struct sockaddr *addr,
            int addrlen);

    extern int recv (NDK_HANDLE handle,
            void *buff,
            size_t len,
            int flags);

    extern int recvfrom (NDK_HANDLE handle,
            void *buff,
            size_t len,
            int flags,
            struct sockaddr *from, 
            int *fromlen);

    extern int send (NDK_HANDLE handle, 
            const void *buff, 
            size_t len, 
            int flags); 

    extern int sendto (NDK_HANDLE handle,
            const void *buff,
            size_t len,
            int flags,
            const struct sockaddr *to, 
            int tolen);

    extern int readv (NDK_HANDLE handle, 
            iovec iov[], 
            size_t count); 

    extern int writev (NDK_HANDLE handle, 
            const iovec iov[], 
            size_t count); 

    extern int setsock_reuseaddr (NDK_HANDLE handle,
            int val = 1);

    extern int setsock_rcvbuf (NDK_HANDLE handle,
            size_t size);

    extern int setsock_sndbuf (NDK_HANDLE handle,
            size_t size);

    extern int getsock_error (NDK_HANDLE handle, 
            int &err);

    // = File System I/O
    extern int open (const char *filename,
            int flags,
            mode_t mode);

    extern int open (const char *filename,
            int flags);

    extern int read (NDK_HANDLE handle, 
            void *buff, 
            size_t len); 

    extern int write (NDK_HANDLE handle, 
            const void *buff, 
            size_t len); 

    extern int close (NDK_HANDLE handle);

    extern int stat (const char *name,
            ndk_stat *st_buf);

    extern int mkdir (const char *name,
            mode_t );

    int rename (const char *old_name,
            const char *new_name);
}

#include "NDK_OS.inl"
#include "Post.h"
#endif

